<template>
  <div v-loading.fullscreen.lock="fullscreenLoading" class="app-container">
    <el-form ref="queryForm" :model="queryParams" :inline="true" label-width="68px">
      <el-form-item label="商品分类" prop="name">
        <el-cascader
          v-model="value"
          :options="options"
          :props="props"
          @change="handleChange"
        />
      </el-form-item>
      <el-form-item label="商品搜索" prop="name">
        <el-input
          v-model="queryParams.arbitrary_field"
          placeholder="商品名称/商品品牌/关键词"
          clearable
          size="small"
          style="width: 200px;"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >添加顶级分类</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="synchronous"
        >分类同步</el-button>
      </el-col>
    </el-row>

    <el-table
      v-loading="loading"
      :data="list"
      row-key="id"
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
      :default-expand-all="false"
    >
      <el-table-column prop="goods_spu" label="商品SPU" align="center" />
      <el-table-column prop="trade_type_name" label="贸易类型" align="center" />
      <el-table-column prop="brand_name" label="品牌" align="center" />
      <el-table-column prop="goods_name" label="商品名称" align="center" />
      <el-table-column label="创建时间" align="center" prop="created_at" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            type="primary"
            @click="handleEdit(scope.row)"
          >修改
          </el-button>
          <el-button
            type="danger"
            @click="handleDel(scope.row)"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="queryParams.page" :limit.sync="queryParams.limit" @pagination="getList" />
    <!-- 分类同步 -->
    <synchronous :open.sync="open" />
    <!-- 新增 -->
    <add :open1.sync="open1" :title="title" />
  </div>
</template>
<script>
import { getShopGoods, getCategory } from '@/api/product'
import synchronous from './synchronous'
import add from './add'
export default {
  name: 'Index',
  components: {
    synchronous,
    add
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      open: false,
      open1: false,
      title: '',
      // 表格数据
      list: [],
      form: {},
      selectData: [],
      // 查询参数
      queryParams: {
        page: 1,
        limit: 10,
        arbitrary_field: undefined
      },
      show: false,
      ids: [],
      fullscreenLoading: false,
      total: 0,
      value: '',
      options: [],
      props: {
        value: 'id',
        label: 'name',
        children: 'children'
      }
    }
  },
  created() {
    this.getList()
    this.getCategory()
  },
  methods: {
    getCategory() {
      getCategory().then(response => {
        if (response.code === 10000) {
          this.options = response.data
        }
      })
    },
    handleChange(value) {
      // console.log(value)
    },
    synchronous() {
      this.open = true
    },
    /** 查询列表 */
    getList() {
      const that = this
      this.loading = true
      getShopGoods(this.queryParams).then(response => {
        if (response.code === 10000) {
          that.list = JSON.parse(JSON.stringify(response.data))
          that.loading = false
          that.total = response.count
        }
      })
    },
    /** 新增按钮操作 */
    handleAdd() {
      // this.form = {}
      this.open1 = true
      this.title = '添加顶级分类'
    },
    /** 编辑按钮操作 */
    handleEdit(row) {
      // console.log(row)
      this.form = {
        id: row.id,
        ids: row.id,
        name: row.name,
        isstatus: row.isstatus,
        parent_id: row.parent_id
      }
      this.open = true
      this.title = '编辑'
    },
    /** 删除按钮操作 */
    handleDel(row) {
      const that = this
      that.$confirm('是否删除该库位,如果该库位底下还有库位则一起删除,确定吗？？？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        that.loading = true
        // eslint-disable-next-line no-undef
        locationDel({ id: row.id.toString() }).then(response => {
          that.loading = false
          if (response.code === 10000) {
            that.$message.success(response.message)
            that.getList()
          } else {
            that.$message.error(response.message)
          }
        })
      }).catch(() => {
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      // this.queryParams.category_id 等于 this.value 的最后一个值
      if (this.value.length > 0) {
        this.queryParams.category_id = this.value[this.value.length - 1]
      }
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams = {
        name: undefined
      }
      this.handleQuery()
    }
  }
}
</script>

